<?php


namespace app\api\model;

use app\api\controller\Code;
use think\Db;
use think\Model;
require_once '../vendor/tencent/tls-sig-api-v2/src/TLSSigAPIv2.php';
/**
 * 腾讯聊天IM
 * Class TengXunModel
 * @package app\api\model
 *
 */
class TengXunModel extends Model
{

//    /**
//     * 设置表名
//     * @var string
//     */
//    protected $table = 'ah_user';
    const APPID = '1600043945';
    const SECRETKEY = '45252393998da40770f74bc41a0d75de68cacdf948665811662d5e77820c9ca4';
    const USERID = 'administrator';

    /**
     *
     * 注册腾讯IM账号
     */
    public static function tengXunIMAdd($params){

        $userInfo = UserModel::where(['id'=>$params['id']])->find();
        $data = [
            'UserID'=>(string)$params['id'],
            'Nick'=>isset($params['nickname']) ? $params['nickname'] :  $userInfo['nickname'] != '' ? $userInfo['nickname'] : 'AI问诊'.$params['id'] ,
            'FaceUrl'=>isset($params['head_img']) ? $params['head_img'] :  $userInfo['head_img'] != '' ? $userInfo['head_img'] : 'https://aiwenzhen.oss-cn-chengdu.aliyuncs.com/20240701/IMAGES/2024-07-01/1719827478115.png' ,
        ];
        //$config = config('tenXunIm');
        //$api = new \Tencent\TLSSigAPIv2( $config['SDKAppID'], $config['secretKey'] );

        $url = self::url('https://console.tim.qq.com/v4/im_open_login_svc/account_import');

//        $res = \HttpHelper::POST($url,json_encode($data));
        $res = postJsonCurl($data,$url,1,[]);
        $res = json_decode($res,true);
        if ($res['ActionStatus'] == 'OK' && $res['ErrorCode'] == 0){

            return true;
        }else{

            return false;
        }


    }

    /**
     *
     * 创建会议
     */
    public static function createMeeting($id,$start_time){

        $data = [

            'RoomInfo'=>[

                'RoomId'=>'ROOM'.mt_rand(10000,99999).date('His'),
                'RoomType'=>'Conference',
                'Owner_Account'=>(string)$id,
                'ScheduleStartTime'=>strtotime($start_time),
            ],


        ];
        $url  = self::url('https://console.tim.qq.com/v4/room_engine_http_srv/create_room');
        $res = postJsonCurl($data,$url,1,[]);
        $res = json_decode($res,true);
        if ($res['ActionStatus'] == 'OK' && $res['ErrorCode'] == 0){

            return $data['RoomInfo']['RoomId'];
        }else{

            return false;
        }

    }

    /**
     *
     * 结束会议
     */
    public static function meetingDel($roomId){
        $data = [

            'RoomId'=>$roomId,
        ];
        $url  = self::url('https://console.tim.qq.com/v4/room_engine_http_srv/destroy_room');
        $res = postJsonCurl($data,$url,1,[]);
        $res = json_decode($res,true);

        $res1 = MeetingModel::meetingEdit(['meeting_id'=>$roomId],['is_end'=>1,'update_at'=>time()]);
        if ($res['ActionStatus'] == 'OK' && $res['ErrorCode'] == 0 && $res1){

            return true;
        }else{

            return false;
        }

    }

    /**
     *
     * 请求连接封装
     */
    public static function url($url){

        $sig = self::getUserSig(self::USERID);
        $random = rand(100000,999999);
        return $url."?sdkappid=".self::APPID."&identifier=".self::USERID."&usersig={$sig}&random={$random}&contenttype=json";
    }


    /**
     *
     * 获取用户的usersig
     */
    public static function getUserSig($id){

        $api = new \Tencent\TLSSigAPIv2(self::APPID,self::SECRETKEY);
        return $api->genUserSig($id);
    }






}